<template>
  <div class="product-bottom-bar">
    <div class="btns-container">
      <!-- 收藏图标 -->
      <div class="collect-icon-container" @click="handleCollect">
        <van-icon
            name="star"
            class="collect-icon"
            :color="isCollected ? '#ffd700' : '#999'"
            :size="24"
        />
        <span class="collect-text">{{ isCollected ? '已收藏' : '收藏' }}</span>
      </div>

      <!-- 联系卖家（优化后） -->
      <div class="contact-container" @click="handleContact">
        <van-icon name="message-o" class="contact-icon" />
        <span class="contact-text">联系卖家</span>
      </div>

      <!-- 立即购买 -->
      <van-button
          type="primary"
          class="buy-btn"
          @click="handleBuy"
      >
        立即购买
      </van-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineEmits, ref ,watch } from "vue";
import { Button, Icon } from "vant";

// 定义触发的事件
const emit = defineEmits([
  "contact", // 联系卖家
  "buy",     // 立即购买
  "collect"  // 收藏状态变化
]);

// 1. 新增：接收父组件传递的 isCollected props
const props = defineProps({
  isCollected: {
    type: Boolean,
    default: false, // 父组件没传时默认未收藏
  },
});

// 收藏状态管理
const isCollected = ref(props.isCollected);

watch(
    () => props.isCollected,
    (newVal) => {
      isCollected.value = newVal;
    }
);

// 收藏点击事件
const handleCollect = () => {
  isCollected.value = !isCollected.value;
  emit("collect", isCollected.value);
};

// 联系卖家点击事件
const handleContact = () => {
  emit("contact");
};

// 立即购买点击事件
const handleBuy = () => {
  emit("buy");
};
</script>

<style scoped>
/* 底部栏容器 */
.product-bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 12px 24px;
  background-color: #fff;
  border-top: 1px solid #f5f5f5;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.06);
  box-sizing: border-box;
  z-index: 99;
}

/* 按钮容器：采用弹性布局 */
.btns-container {
  display: flex;
  align-items: center;
  gap: 16px;
  width: 100%;
}

/* 收藏图标容器 */
.collect-icon-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  cursor: pointer;
  width: 60px; /* 固定宽度，避免布局跳动 */
}

.collect-icon {
  transition: color 0.3s ease;
}

.collect-text {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

/* 联系卖家（优化样式） */
.contact-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  cursor: pointer;
  background-color: #f7f8fa;
  border-radius: 12px; /* 圆角更柔和 */
  flex: 1; /* 自适应空间 */
  height: 50px;
  transition: all 0.2s ease; /* 统一过渡动画 */
}

.contact-icon {
  font-size: 20px;
  color: #36cbcb; /* 用品牌蓝绿色，区别于收藏和购买 */
  margin-bottom: 2px;
}

.contact-text {
  font-size: 12px;
  color: #333;
  font-weight: 500;
}

/* 联系卖家交互反馈 */
.contact-container:active {
  background-color: #eef0f3;
  transform: scale(0.98); /* 轻微缩小，增强点击感 */
}

/* 立即购买按钮 */
.buy-btn {
  flex: 2; /* 保持主按钮占比 */
  height: 50px !important;
  font-size: 16px !important;
  border-radius: 12px !important;
  background-color: #ff4d4f !important;
  color: #fff !important;
  border: none !important;
  transition: all 0.2s ease;
}

.buy-btn:active {
  background-color: #e03a3a !important;
  transform: scale(0.98);
}

/* 收藏点击反馈 */
.collect-icon-container:active {
  transform: scale(0.95);
}
.collect-icon-container:active .collect-text {
  color: #333;
}
</style>
